/*    */ package com.wgcloud.util;
/*    */ 
/*    */ import java.io.BufferedReader;
/*    */ import java.io.IOException;
/*    */ import java.io.InputStreamReader;
/*    */ import org.apache.commons.lang3.StringUtils;
/*    */ import org.slf4j.Logger;
/*    */ import org.slf4j.LoggerFactory;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class PingUtil
/*    */ {
/* 21 */   private static final Logger logger = LoggerFactory.getLogger(com.wgcloud.util.PingUtil.class);
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */   
/*    */   public static long ping(String ipAddress, int count, int timeOut) {
/*    */     String pingCommand;
/* 33 */     BufferedReader in = null;
/*    */     
/* 35 */     Runtime r = Runtime.getRuntime();
/* 36 */     String osName = System.getProperty("os.name");
/* 37 */     if (osName.contains("Windows")) {
/* 38 */       pingCommand = "ping " + ipAddress + " -n " + count + " -w " + timeOut;
/*    */     } else {
/* 40 */       pingCommand = "ping  -c " + count + " -w " + timeOut + " " + ipAddress;
/*    */     } 
/*    */     try {
/* 43 */       long startTimes = System.currentTimeMillis();
/* 44 */       Process p = r.exec(pingCommand);
/* 45 */       long endTimes = System.currentTimeMillis();
/* 46 */       long diffTimes = endTimes - startTimes;
/* 47 */       if (p == null) {
/* 48 */         return -1L;
/*    */       }
/* 50 */       in = new BufferedReader(new InputStreamReader(p.getInputStream()));
/*    */       
/* 52 */       boolean ttlSign = false; String line;
/* 53 */       while ((line = in.readLine()) != null) {
/* 54 */         ttlSign = getCheckResult(line);
/* 55 */         if (ttlSign) {
/*    */           break;
/*    */         }
/*    */       } 
/* 59 */       if (ttlSign) {
/* 60 */         return diffTimes;
/*    */       }
/* 62 */     } catch (Exception ex) {
/* 63 */       logger.error("数通设备ping错误：", ex);
/* 64 */       return -1L;
/*    */     } finally {
/*    */       try {
/* 67 */         in.close();
/* 68 */       } catch (IOException e) {
/* 69 */         e.printStackTrace();
/*    */       } 
/*    */     } 
/* 72 */     return -1L;
/*    */   }
/*    */   
/*    */   private static boolean getCheckResult(String line) {
/* 76 */     if (StringUtils.isEmpty(line)) {
/* 77 */       return false;
/*    */     }
/* 79 */     line = line.toLowerCase();
/* 80 */     if (line.contains("ttl=")) {
/* 81 */       return true;
/*    */     }
/* 83 */     return false;
/*    */   }
/*    */ }


/* Location:              /Users/zhangyongxin/soft-install/wgcloud/wgcloud-v3.3.5/server/bank/wgcloud-server-release.jar!/BOOT-INF/classes/com/wgcloud/util/PingUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */